<template>
  <basic-container>
    <avue-form :option="companyOption" v-model="form" :upload-delete="uploadDelete" :upload-before="uploadBefore"
               :upload-after="uploadAfter" @submit="submit"></avue-form>
  </basic-container>
</template>

<script>
import {getDetail, update} from "@/api/enterprise/companybasicinf";
import {baseUrl} from "@/config/env";

export default {
  data() {
    const validateProgramKey = (rule, value, callback)=>  {
      //密码强度正则，最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符
      const p = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
      if (value === '') {
        callback()
      }
      if (!p.test(value)) {
        callback(new Error('请输入最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符的企业密钥'))
      } else {
        callback()
      }
    }
    return {
      form: {
        photos: ''
      },
      companyOption: {
        column: [
          {
            type: 'title',
            span: 24,
            display: true,
            styles: {
              fontSize: '25px',
              color: '#000',
              fontWeight: '400'
            },
            labelWidth: '0px',
            value: '企业信息',
            prop: '',
            arrow: false,

            collapse: true,
            children: {
              column: []
            },
            label: ''
          },
          {
            title: '分割线',
            component: 'el-divider',
            span: 24,
            tips: '看我：自定义属性怎么用？',
            labelWidth: '0px',
            params: {},
            prop: '1628036675681_81617'
          },
          {
            type: 'title',
            span: 24,
            display: true,
            styles: {
              fontSize: '18px',
              color: '#032FF7'
            },
            labelWidth: '0px',
            value: '企业基本信息',
            prop: '1628036703779_25958'
          },
          {
            type: 'input',
            label: '用户名',
            span: 24,
            disabled:true,
            display: true,
            prop: 'userName'
          },
          {
            type: 'input',
            label: '企业名称',
            span: 24,
            disabled:true,
            display: true,
            prop: 'companyName'
          },
          {
            type: 'input',
            label: '组织代码',
            span: 24,
            disabled:true,
            display: true,
            prop: 'creditCode'
          },
          {
            type: 'select',
            label: '单位性质',
            dicUrl: baseUrl + '/blade-system/dict/dictionary?code=company_type',
            span: 13,
            display: true,
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            prop: 'companyType'
          },
          {
            type: 'select',
            label: '企业注册类型',
            dicUrl: baseUrl + '/blade-system/dict/dictionary?code=company_register_type',
            span: 11,
            display: true,
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            prop: 'companyRegisterType'
          },
          {
            type: 'select',
            label: '所属行业',
            dicUrl: baseUrl + '/blade-system/dict/dictionary?code=industry_type',
            span: 13,
            display: true,
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            prop: 'industryType'
          },
          {
            type: 'input',
            label: '通讯地址',
            span: 24,
            display: true,
            prop: 'contactAddress'
          },
          {
            type: 'select',
            label: '控股情况',
            dicUrl: baseUrl + '/blade-system/dict/dictionary?code=holding_situation',
            span: 13,
            display: true,
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            prop: 'holdingSituation'
          },
          {
            type: 'textarea',
            label: '企业介绍',
            span: 24,
            display: true,
            prop: 'companyIntroduce'
          },
          {
            type: 'textarea',
            label: '主营产品',
            span: 24,
            display: true,
            prop: 'mainProducts'
          },
          {
            label: '企业相册',
            prop: 'photos',
            type: 'upload',
            dataType: 'string',
            propsHttp: {
              res: 'data',
            },
            span: 24,
            listType: 'picture-card',
            tip: '只能上传jpg/png文件，且不超过500kb',
            action: baseUrl + '/blade-resource/local/endpoint/arrayFileUpload-file'
          },
          {
            type: 'title',
            span: 16,
            display: true,
            styles: {
              fontSize: '19px',
              color: '#063BF8',
              fontWeight: '400'
            },
            labelWidth: '0px',
            value: '联系信息',
            prop: '1628037308747_90190'
          },
          {
            type: 'input',
            label: '法人代表',
            span: 16,
            disabled:true,
            display: true,
            prop: 'legalRepresentative'
          },
          {
            type: 'input',
            label: '电话',
            disabled:true,
            span: 16,
            display: true,
            prop: 'phone'
          },
          {
            type: 'input',
            label: '邮箱',
            span: 16,
            display: true,
            prop: 'email'
          },
          {
            type: 'input',
            label: '网址',
            span: 16,
            display: true,
            prop: 'url'
          },
          {
            type: 'input',
            label: '传真',
            span: 16,
            display: true,
            prop: 'fax'
          },
          {
            type: 'input',
            label: '企业密钥',
            span: 16,
            display: true,
            prop: 'programKey',
            rules: [{ validator: validateProgramKey, trigger: 'blur' }]
          }
        ],
        labelPosition: 'right',
        labelSuffix: '：',
        labelWidth: 120,
        gutter: 0,
        menuBtn: true,
        submitBtn: true,
        submitText: '提交',
        emptyBtn: true,
        emptyText: '清空',
        menuPosition: 'center'
      }
    }
  },
  created() {
    // 获取当前用户
    getDetail().then((res) => {
      this.form = res.data.data
    }, error => {
      window.console.log("错误啦", error);
    })
  },
  methods: {
    uploadBefore(file, done) {
      done();
    },
    uploadAfter(res, done) {
      // 放行
      done();
    },     // 删除图片
    uploadDelete() {
      return this.$confirm(`是否确定移除该选项？`);
    },
    submit(form, done) {
      // 保存数据
      update(form).then(() => {
        this.$message.success('保存成功');
      }, () => {
        this.$message.success('保存失败,请联系管理员');
      });
      done()
    }
  }

}

</script>
